<?php defined('SYSPATH') or die('No direct script access.');

class Model_Services_Patient extends Model implements Jqgrid_GridListener{

	public $patient = NULL;


	public function __construct($id = false){
		if ($id) $this->patient = $this->get($id);
	}

	public function newPatient(){
		$new = ORM::factory("dao_patient");
		$new->doctype_id = 0;
		$new->identity_card_number = "";
		$new->surname = "";
		$new->name = "";
		$new->birthdate = "2011-01-01";
		$new->address = "";
		$new->phone = "";
		$new->cel = "";
		$new->email = "";
		$new->sex = "";
		$new->city = "";
		$new->profession_id = 0;
		$new->medic_id = null;
		$new->habits_id = null;
		$this->patient = $new;
		return;
	}

	public function count($criteria = false){
		return ORM::factory("dao_patient")->count_all();
	}

	public function getList($criteria = false){

		if (!$criteria)	$patient_list = ORM::factory("dao_patient")->find_all()->as_array();
		else{
			if ($criteria->isSearch){

				if ($criteria->searchField == 'doctype'){
					$criteria->searchField = 'id';
					$tablename = $criteria->getSQLField("doctype");
				}else{
					$tablename = $criteria->getSQLField("patients");
				}

				$patient_list = ORM::factory("dao_patient")
				->with('doctype')
				->where($tablename, $criteria->getSQLOper(), $criteria->getSQLSearchString())
				->order_by($criteria->sidx,$criteria->sord)
				->limit($criteria->limit)
				->offset($criteria->calcOffset())
				->find_all()
				->as_array();

			}else{
				$patient_list = ORM::factory("dao_patient")->order_by($criteria->sidx,$criteria->sord)->limit($criteria->limit)->offset($criteria->calcOffset())->find_all()->as_array();
			}
		}

		$dataset = array();
		foreach ($patient_list as $patient){
			$dataset[$patient->id] = array(	$patient->surname,
			$patient->name,
			$patient->doctype->description,
			$patient->identity_card_number);
		}
		return $dataset;
	}

	/**
	 *
	 * get all patients from an id
	 * @param int $minId
	 * @param int $maxAmount
	 * @return array of patients
	 */
	public function getListFromMinID($minId, $maxAmount){
		return ORM::factory("dao_patient")->where("id",">",$minId)->order_by("id", "ASC")->limit($maxAmount)->find_all()->as_array();
	}

	public function get($id){
		return ORM::factory("dao_patient")->find($id);
	}
	public function getFullname(){
		return $this->patient->surname.", ".$this->patient->name;
	}
	public function getAge(){
		return Helper_Date::determineAge($this->patient->birthdate);
	}
	public function smokes(){
		return ($this->patient->habits->smoke)?"Si":"No";
	}

	public function getSurname(){
		return $this->patient->surname;
	}
	public function getName(){
		return $this->patient->name;
	}
	public function getBirthdate(){
		return Helper_Date::mysqlToDate($this->patient->birthdate);
	}
	public function getGenre(){
		return $this->patient->sex;
	}
	public function getDoctype(){
		return Helper_Service::factory("identitycard")->get($this->patient->doctype_id);
	}
	public function getIdentityCardNumber() {
		return $this->patient->identity_card_number;
	}
	public function getProfession(){
		return Helper_Service::factory("profession")->get($this->patient->profession_id);
	}
	public function getAddress(){
		return $this->patient->address;
	}
	public function getPhone(){
		return $this->patient->phone;
	}
	public function getCellphone(){
		return $this->patient->cel;
	}
	public function getEmail(){
		return $this->patient->email;
	}
	public function getCity(){
		return $this->patient->city;
	}


	public function save($id, array $data){
		return 	$this->savePersonalInfo($id,$data);
	}

	public function delete($id){
	}


	public function getFamilyHistory(){
		return ($this->patient) ? Helper_Service::factory("familypatient",$this->patient->habits_id) : NULL;
	}

	public function getHabits(){
		return ($this->patient) ? Helper_Service::factory("habit",$this->patient->habits_id) : NULL;
	}

	public function getMedicines(){
		return ($this->patient) ? Helper_Service::factory("sportpatient",$this->patient->id) : NULL;
	}

	public function getMonitoring(){
		if ($this->patient){
			$service = Helper_Service::factory("clinicalinformation");
			$service->setPatient($this->patient->id);
			return $service;
		}else{
			return NULL;
		}
	}

	public function getPathologies(){
		return ($this->patient) ? Helper_Service::factory("pathologypatient",$this->patient->id) : NULL;
	}

	//mmmm no esta del todo mal, pero solo seria accesible desde los web services
	public function getSports(){
		return ($this->patient) ? Helper_Service::factory("sportpatient",$this->patient->id) : NULL;
	}

	public function getSurgeries(){
		return ($this->patient) ? Helper_Service::factory("surgerypatient",$this->patient->id) : NULL;
	}

	public function savePersonalInfo($id,$data){

		$this->patient = ($id > 0) ? ORM::factory("dao_patient",$id) : ORM::factory("dao_patient");

		//hacer funcion para pasar de string a numeros (una funcion sanitize input)
		$this->patient->doctype_id =$data['doctype'];
		$this->patient->identity_card_number = $data['identity_card_number'];
		$this->patient->surname = $data['surname'];
		$this->patient->name = $data['name'];
		$this->patient->birthdate = Helper_Date::dateToMysql($data['birthdate']);
		$this->patient->address = $data['address'];
		$this->patient->phone = $data['phone'];
		$this->patient->cel = $data['cel'];
		$this->patient->email = $data['email'];
		$this->patient->sex = ($data['sex'] == "Masculino") ? "M" : "F";
		$this->patient->city = $data['city'];
		
		if ($data['profession']){
			$this->patient->profession_id = $data['profession'];	
		}
		
		


		//$this->patient->medic_id = $data['medic'];
		//$this->patient->habits_id = $data['habits'];

		$this->patient->save();
		return $this->patient->id;


	}


	public function saveFamilyHistory($id,$data){
		return Helper_Service::factory();
	}

	public function saveHabits($id,$data){
		return Helper_Service::factory();
	}

	public function saveMedicines($id,$data){
		return Helper_Service::factory();
	}

	public function saveMonitoring($id,$data){
		return Helper_Service::factory();
	}

	public function savePathologies($id,$data){
		return Helper_Service::factory();
	}


	public function saveSurgeries($id,$data){
		return Helper_Service::factory();
	}

}
?>